package com.fairytask.ui
{
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.resource.StaticIni;
	import com.fairytask.ui.base.FrameUI;
	import com.fairytask.ui.item.InfoItem;
	
	import flash.events.MouseEvent;
	
	/**
	 *购买商品数量输入框 
	 * @author pangj
	 * 
	 */
	public class InputAmountUI extends FrameUI
	{
		private static var _self       :InputAmountUI=null;
		private var        amount       :uint;     //购买商品的数量
		public var         info         :InfoItem; //购买输入框
		public var         sureBtn      :Button;   //确认按钮
		public var         good         :Object;   //需购买的商品
		public var         haveMoney    :int;      //玩家的金钱

		/**
		 * 初始化窗口
		 * 
		 */
		public function InputAmountUI(popUI:Boolean=true)
		{
			super(popUI);
			sureBtn.addEventListener(MouseEvent.CLICK,sendAmount);
		}
		public static function getInstance():InputAmountUI
		{
			if(_self==null)
			{
				_self=new InputAmountUI();
			}
			return _self;
		}
		public function setGoodInfo(good:Object,haveMoney:int):void
		{
			windowBG.visible = true;
			this.good=good;
			if(good!=null)
			{
				this.haveMoney=haveMoney;
				info = new InfoItem(judge,StaticIni.getSwfLangStr("1000000120"));
				info.x=30;info.y=60;
				windowBG.addChild(info);
			}
		}
		/**
		 *向服务端发送购买信息 
		 * 
		 */
		public function sendAmount(e:MouseEvent):void
		{
			windowBG.visible = false;
			if(good!=null)
			{
				if(info.parent)
					info.parent.removeChild(info);
				//发送商品id及购买数量
				//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_SHOPPING_ITEM_BUY,good.id,amount);
			}
		}
		/**
		 * 判断处理输入的信息
		 * 
		 */
		public function judge(i:int):void
		{
			//设置最大输入量
			if(int(info.text)*good.price>haveMoney)
			{
				info.text=String(int(haveMoney/good.price));
			}
			if(int(info.text)>99)
			{
				info.text=String(99);
			}
			amount=int(i);
		}
	}
}